<?php
// +----------------------------------------------------------------------
// | HTOMS
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2018 http://www.njhtjr.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangjian <446180102@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Model;
use wechat\send;
/**
 * 部门模型
 *
 * 
 */
class Dept extends Model
{   
  
    

    
    /*
     * 根据部门编号获取最小层级部门信息
     * @param int $deptId 部门编号
     * @return array 部门数组列表
     */
    public function getChildDeptIdsByDeptid($deptId)
    {
        $childDeptIdarr = array();
        $table = db('dept');
        $dept_info = $table->find($deptId);
        
        $ids = array();
        if(is_array($dept_info) && !empty($dept_info))
        {
            $allDeptList = array();
            $allDeptList = $table->where('org_id', $dept_info['org_id'])->field('id, pid')->select();

            $childDeptIdarr = self::getChildNode($ids, $allDeptList, $dept_info['id']);
            
        }
        
        return $childDeptIdarr;
    }
    
    
    private function getChildNode(&$ids,$data, $pid)
    {    
        foreach ($data as $key => $value) {
            if($value['pid'] == $pid) {
                $ids[] = $value['id'];
                self::getChildNode($ids,$data, $value['id']);
            }
        }
        return $ids;
    }

    /*
     * 根据公司编号获取部门列表
     * @param int $org_ids 公司ID
     * @param int $search_field 搜索字段
     * @return array 部门数组列表
     */
    public function getDeptListByOrgIds($org_id = 0)
    {
        $return_list = array();
        if(isset($org_id) && !empty($org_id)){
            $where_cond = array(
                'org_id' => $org_id,
                'isvalid' => 1
            );
            $return_list = db('dept')->where($where_cond)->field('id, dept_name')->select();
        }
        return $return_list;
    }

}